package study.zhangbo.lock.tests;

import lombok.extern.slf4j.Slf4j;
import study.zhangbo.lock.distributed.IBaseLockerService;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TestThread implements Runnable {

    private IBaseLockerService baseLockerService;

    public TestThread(IBaseLockerService baseLockerService) {
        this.baseLockerService = baseLockerService;
    }

    @Override
    public void run() {
        try {
            baseLockerService.acquireLock();
            log.info("开始执行逻辑：{}", Thread.currentThread().getName());
            if (Thread.currentThread().getName().contains("2") ||
                    Thread.currentThread().getName().contains("4") ||
                    Thread.currentThread().getName().contains("6") ||
                    Thread.currentThread().getName().contains("8") ||
                    Thread.currentThread().getName().contains("0")
                    ) {
                throw new RuntimeException("手动异常");
            }
            TimeUnit.SECONDS.sleep(3);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.info("逻辑执行完成：{}", Thread.currentThread().getName());
            baseLockerService.releaseLock();
        }


    }

}